package com.gxuwz.leaveSystem.dao;

import com.gxuwz.leaveSystem.entity.Department;
import com.gxuwz.leaveSystem.entity.Instructor;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class InstructorDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 添加
     * @param ins
     * @return
     */
    public int add(Instructor ins){
        String sql = "insert into sys_instructor(instID, instName, depID, telephone) value(?,?,?,?)";
        Object[] obj = new Object[] {
                ins.getInstID(),
                ins.getInstName(),
                ins.getDepID(),
                ins.getTelephone()
        };
        int num = this.jdbcTemplate.update(sql,obj);
        return num;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(String id){
        String sql = "delete from sys_instructor where instID=?";
        int num = this.jdbcTemplate.update(sql,id);
        return num;
    }

    /**
     * 修改
     * @param ins
     * @return
     */

    public int update(Instructor ins){
        String sql = "update sys_instructor set instName = ?, depID = ?,telephone = ? where instID = ? ";
        Object[] up = new Object[] {
                ins.getInstName(),
                ins.getDepID(),
                ins.getTelephone(),
                ins.getInstID()
        };
        int num = this.jdbcTemplate.update(sql,up);
        return num;
    }

    /**
     * 根据id查列表
     * @param id
     * @return
     */

    public Instructor findByid(String id){
        String sql = "select * from sys_instructor where instID = ?";
        RowMapper<Instructor> rowMapper = new BeanPropertyRowMapper<Instructor>(Instructor.class);
        List<Instructor> ins = this.jdbcTemplate.query(sql, rowMapper, id);
        if (ins.isEmpty()) {
            return null;
        } else {
            return ins.get(0);
        }
    }

    /**
     * 查询全部
     * @return
     */
    public List<Instructor> findAll(){
        String sql = "select * from sys_instructor";
        RowMapper<Instructor> rowMapper = new BeanPropertyRowMapper<Instructor>(Instructor.class);
        return  this.jdbcTemplate.query(sql,rowMapper);
    }

    /**
     * 根据关键字搜索
     * @param keyword
     * @return
     */
    public List<Instructor> search(String keyword) {
        String sql = "SELECT * FROM sys_instructor WHERE sys_instructor.instID LIKE ? OR sys_instructor.instName LIKE ? OR sys_instructor.depID LIKE ? OR sys_instructor.telephone LIKE ?";
        return jdbcTemplate.query(sql, new Object[]{"%" + keyword + "%", "%" + keyword + "%","%" + keyword + "%","%" + keyword + "%"}, new BeanPropertyRowMapper<>(Instructor.class));
    }
}
